package com.css.fxfzysx.modules.vaolEngineering.repository.impl;

import cn.hutool.core.util.StrUtil;
import com.css.fxfzysx.constants.FxfzConstants;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxMainTableEntity;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxVersionEntity;
import com.css.fxfzysx.modules.lifeline.vo.SmxVersionVo;
import com.css.fxfzysx.modules.vaolEngineering.param.PageParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.ValEngineeringRepository;
import com.css.fxfzysx.modules.vaolEngineering.vo.PageVo;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Repository
public class ValEngineeringRepositoryImpl implements ValEngineeringRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public HashMap<String, Object> page(PageParam param, int page, int size,List<String> roleList) {
        StringBuilder sql = new StringBuilder("select ct.*,cv.version_code,cv.bridge_type from czt_smx_main_table ct left join czt_smx_version cv on ct.version_id = cv.id where ct.del_flag = '0' and ct.type is not null and ct.calculation_status != 0  ");
        if (!roleList.contains(FxfzConstants.CZT_COUN_GL_ROLE) && !roleList.contains(FxfzConstants.CZT_PRI_GL_ROLE)) {
            sql.append(" and ct.create_user ='" + PlatformSessionUtils.getUserId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getType())) {
            sql.append(" and ct.type = '"+param.getType()+"'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getUserName())) {
            sql.append(" and ct.create_user_name like '%" + param.getUserName() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getYear())) {
            if(param.getYear() == 1){
                sql.append(" and ct.year < 1978");
            }else if(param.getYear() == 2){
                sql.append(" and ct.year between 1978 and 1989");
            }else if(param.getYear() == 3){
                sql.append(" and ct.year between 1989 and 2001");
            }else if(param.getYear() == 4){
                sql.append(" and ct.year > 2001");
            }
        }
        if (!PlatformObjectUtils.isEmpty(param.getStartTime())) {
            sql.append(" and ct.create_time >= '" + param.getStartTime() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getEndTime())) {
            String endTime = param.getEndTime() + " 23:59:59";
            sql.append(" and ct.create_time <='" + endTime + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getFortification())) {
            sql.append(" and ct.fortification ='" + param.getFortification() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(param.getStatus())) {
            sql.append(" and ct.calculation_status ='" + param.getStatus() + "'");
        }
        StringBuilder orderSql = new StringBuilder(" order by ct.create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PageVo.class)).size();
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(orderSql).append(limitSql);
        List<PageVo> mtolList = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(PageVo.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", mtolList);
        return resultMap;
    }

    @Override
    public String getCodeByType(String type) {
        try {
            String sql = "select code from czt_smx_main_table where del_flag = '0' and business_object = '"+type+"' order by create_time desc limit 1";
            return jdbcTemplate.queryForObject(sql, String.class);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveMainTable(SmxMainTableEntity entity) {
        jdbcTemplate.update("insert into czt_smx_main_table  (id,code,business_object,year,fortification,create_user,create_user_name,create_time,del_flag,org_name,calculation_status,version_id,type,model_ids1,model_ids2,device_type,floor) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )",
                entity.getId(),entity.getCode(),entity.getBusinessObject(),entity.getYear(),entity.getFortification(),entity.getCreateUser(),entity.getCreateUserName(),entity.getCreateTime(),entity.getDelFlag(),entity.getOrgName(),entity.getCalculationStatus(),entity.getVersionId(),entity.getType(),entity.getModelIds1(),entity.getModelIds2(),entity.getDeviceType(),entity.getFloor());
    }

    @Override
    public void updateCalculationStatus(String id,String state6,String state7,String state8,String state9,String state10, Integer calculationStatus) {
        if(StrUtil.isEmpty(state6)){
            state6 = "";
        }
        if(StrUtil.isEmpty(state7)){
            state7 = "";
        }
        if(StrUtil.isEmpty(state8)){
            state8 = "";
        }
        if(StrUtil.isEmpty(state9)){
            state9 = "";
        }
        if(StrUtil.isEmpty(state10)){
            state10 = "";
        }
        jdbcTemplate.update("update czt_smx_main_table set calculation_status = "+calculationStatus+",destructive_state6 = '"+state6+"',destructive_state7 = '"+state7+"',destructive_state8 = '"+state8+"',destructive_state9 = '"+state9+"',destructive_state10 = '"+state10+"' where id = '"+id+"'");
    }

    @Override
    public HashMap<String, Object> getVersionByType(Integer type, int page, int size, String userID) {
        StringBuilder sql = new StringBuilder("select * from czt_smx_version where del_flag = '0' and create_user = '"+userID+"' and type = "+type+" and version_status = 1 order by create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SmxVersionVo.class)).size();
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(limitSql);
        List<SmxVersionEntity> mtolList = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SmxVersionEntity.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", mtolList);
        return resultMap;
    }

    @Override
    public void updateModelBaseIds(String modelIds1, String modelIds2,String id) {
        jdbcTemplate.update("update czt_smx_main_table set model_ids1 = '"+modelIds1+"',model_ids2 ='"+modelIds2+"' where id = '"+id+"'");
    }

    @Override
    public void updateCountStatusByModelIds1Id(String id) {
        String sql = "update czt_smx_main_table set calculation_status = '2' where model_ids1 like '%"+id+"%'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateCountStatusByModelIds2Id(String id) {
        String sql = "update czt_smx_main_table set calculation_status = '2' where model_ids2 like '%"+id+"%'";
        jdbcTemplate.update(sql);
    }

    @Override
    public SmxMainTableEntity getById(String id) {
        String sql = "select * from czt_smx_main_table where id = '"+id+"'";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(SmxMainTableEntity.class));
    }

    @Override
    public void updateVersionCalculationStatus(String versionId,Integer status) {
        String sql = "update czt_smx_version set version_status = "+status+" where id = '"+versionId+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void deleteByTypeAndTableId(Integer type, String id,Integer bridgeType) {
        String name = "table_id";
        StringBuilder sql = new StringBuilder("update ");
        if (type == 1) {
            sql.append(" czt_smx_up_network");
        } else if (type == 2) {
            sql.append(" czt_smx_water_supply");
        } else if (type == 3) {
            sql.append(" czt_smx_air_receiver");
        } else if (type == 4) {
            if(bridgeType == 0){
                sql.append(" czt_smx_bridge_point");
            }else {
                sql.append(" czt_smx_bridge_line");
            }
        } else if (type == 5) {
            sql.append(" czt_smx_road");
        } else if (type == 6) {
            sql.append(" czt_smx_transformer_substation");
        } else if (type == 7) {
            name = "project_id";
            sql.append(" czt_pse_vulnerability ");
        } else if (type == 9) {
            sql.append(" czt_smx_devices");
        }
        sql.append(" set del_flag = '1' where "+name+" = '"+id+"'");
        jdbcTemplate.update(sql.toString());
    }

    @Override
    public void updateUpNetCalculationStatus(String tableId, int range) {
        jdbcTemplate.update("update czt_smx_main_table set calculation_status = "+range+" where id = '"+tableId+"'");
    }
}
